<?php

use \app\dao\WechatFriendChatMessageDO;
use \app\dao\WechatGroupChatMessageDO;

class EventContentRouterService extends \app\service\BaseService {

    public static $EVENT_TYPE_CHATMSG = 'chat_message_handler';

    public function __construct() {
        parent::__construct();
        $this->logpath = strtolower(__CLASS__);
    }

    public function execute($eventType, $eventContent) {
        $res = $this->eventRouter($eventType, $eventContent);
        $res->success || $this->recordObject($res);
        return $res;
    }

    public function eventRouter($eventType, $eventContent) {
        try {
            switch ($eventType) {
                case self::$EVENT_TYPE_CHATMSG:
                    return $this->formatChatData($eventContent);
                default:
                    return outputObject(FALSE, '其它第三方回调事件暂不处理', NULL, __FUNCTION__, __LINE__);
            }
        } catch (\Exception $e) {
            return outputObject(FALSE, '数据转发发生未知错误：' . $e->getMessage(), NULL, __FUNCTION__, __LINE__);
        }
    }

    private function formatChatData($content) {
        if (isset($content->Data->vcChatRoomID) && $content->Data->vcChatRoomID) {  //群聊
            return $this->operateGroupMsg($content);
        } else { //私聊
            return $this->operateFriendMsg($content);
        }
    }

    private function operateGroupMsg($content) {
        $wechatFriendChatMessageModel = $this->CORE->loader->model('WechatGroupChatMessageModel');
        $do = new WechatGroupChatMessageDO;
        $do->chatroom_id = trim($content->Data->vcChatRoomID);
        isset($content->vcMerChantNo) && $do->seller_no = trim($content->vcMerChantNo);
        isset($content->Data->vcMsgId) && $do->msg_id = intval($content->Data->vcMsgId);
        isset($content->Data->snSendType) && $do->send_type = intval($content->Data->snSendType);
        isset($content->Data->dtMsgTime) && $content->Data->dtMsgTime && $do->msg_time = strtotime($content->Data->dtMsgTime);
        isset($content->Data->snMsgType) && $do->msg_type = trim($content->Data->snMsgType);
        isset($content->Data->vcCustomerWxID) && $do->friend_id = trim($content->Data->vcCustomerWxID);
        isset($content->Data->vcRobotWxID) && $do->xiaou_id = trim($content->Data->vcRobotWxID);
        isset($content->Data->nCustomerType) && $do->customer_type = intval($content->Data->nCustomerType);
        $do->content = json_encode([
            'text' => isset($content->Data->vcContent) ? $content->Data->vcContent : '',
            'url' => isset($content->vcUrl) ? $content->vcUrl : '',
            'img' => isset($content->vcImg) ? $content->vcImg : '',
            'link_desc' => isset($content->vcLinkDesc) ? $content->vcLinkDesc : '',
            'voice_length' => isset($content->inDuration) ? $content->inDuration : ''
        ], JSON_UNESCAPED_UNICODE | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
        $do->state = WechatGroupChatMessageModel::$STATE_UNREAD;
        return $wechatFriendChatMessageModel->insert($do);
    }

    private function operateFriendMsg($content) {
        $wechatFriendChatMessageModel = $this->CORE->loader->model('WechatFriendChatMessageModel');
        $do = new WechatFriendChatMessageDO;
        isset($content->vcMerChantNo) && $do->seller_no = trim($content->vcMerChantNo);
        isset($content->Data->vcMsgId) && $do->msg_id = intval($content->Data->vcMsgId);
        isset($content->Data->snSendType) && $do->send_type = intval($content->Data->snSendType);
        isset($content->Data->dtMsgTime) && $content->Data->dtMsgTime && $do->msg_time = strtotime($content->Data->dtMsgTime);
        isset($content->Data->snMsgType) && $do->msg_type = trim($content->Data->snMsgType);
        isset($content->Data->vcCustomerWxID) && $do->friend_id = trim($content->Data->vcCustomerWxID);
        isset($content->Data->vcRobotWxID) && $do->xiaou_id = trim($content->Data->vcRobotWxID);
        isset($content->Data->nCustomerType) && $do->customer_type = intval($content->Data->nCustomerType);
        $do->content = json_encode([
            'text' => isset($content->Data->vcContent) ? $content->Data->vcContent : '',
            'url' => isset($content->vcUrl) ? $content->vcUrl : '',
            'img' => isset($content->vcImg) ? $content->vcImg : '',
            'link_desc' => isset($content->vcLinkDesc) ? $content->vcLinkDesc : '',
            'voice_length' => isset($content->inDuration) ? $content->inDuration : ''
        ], JSON_UNESCAPED_UNICODE | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
        $do->state = WechatFriendChatMessageModel::$STATE_UNREAD;
        return $wechatFriendChatMessageModel->insert($do);
    }
}
